<?php

namespace App\Http\Controllers\Admin;

use App\Models\Type;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Rule;

/**
 * 用户分类管理
 * Class TypeController
 * @package App\Http\Controllers\Admin
 */
class TypeController extends BaseController
{
    /**
     * 分类列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $list = Type::paginate(config('site.other.paginate'));
        return backendView('type.index', compact('list'));
    }

    /**
     * 新增分类
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function add(Request $request)
    {
        if ($request->isMethod('POST')) {

            $this->validate($request, [
                'name' => 'required|max:255|unique:types',
                'sort' => 'required|integer',
                'status' =>'required|in:0,1'
            ], [], [
                'name' => '分类名称',
                'sort' => __("Sort"),
                'status' => __("Status"),
            ]);

            $data = $request->only(['name','sort', 'status']);

            if (Type::create($data)) {
                return redirect()->route('admin.type.index')->with('success', __('Create success'));
            }

            return back()->with('error', __('Create failed'))->withInput();
        } else {
            return backendView('type.add');
        }
    }

    /**
     * 编辑分类
     * @param Request $request
     * @param null $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function edit(Request $request, $id = null)
    {
        /**
         * @var $type Type
         */
        $type = Type::find($id);
        if (empty($type)) {
            return back()->with('error', __('The page you requested was not found'))->withInput();
        }
        if ($type->is_system == 1){
            return back()->with('error', '系统分类不能编辑')->withInput();
        }
        if ($request->isMethod('POST')) {

            $this->validate($request, [
                'name' => ['required', 'max:255',Rule::unique('types')->ignore($type->id)],
                'sort' => 'required|integer',
                'status' =>'required|in:0,1'
            ], [], [
                'name' => '分类名称',
                'sort' => __("Sort"),
                'status' => __("Status"),
            ]);

            $data = $request->only(['name','sort', 'status']);

            if ($type->update($data)) {
                return redirect()->route('admin.type.index')->with('success', __('Edit success'));
            }

            return back()->with('error', __('Edit failed'))->withInput();

        } else {
            return backendView('type.edit', compact('type'));
        }
    }

    /**
     * 查看分类
     * @param null $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function view($id = null)
    {
        /**
         * @var $type Type
         */
        $type = Type::find($id);
        if (empty($type)) {
            return back()->with('error', __('The page you requested was not found'));
        }

        return backendView('type.view', compact('type'));
    }

    /**
     * 删除分类
     * @param null $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function delete($id = null)
    {
        /**
         * @var $type Type
         */
        $type = Type::find($id);
        if (empty($type)) {
            return back()->with('error', __('The page you requested was not found'));
        }

        if ($type->is_system == 1){
            return back()->with('error', '系统分类不能删除')->withInput();
        }
        try {
            $users = $type->users()->get();
            $agencies = $type->agencies()->get();

            if (count($users) > 0 || count($agencies) > 0) {
                return back()->with('error', '已使用的分类不能删除');
            }

            if ($type->delete()) {
                return back()->with('success', __('Delete success'));
            }
        } catch (\Exception $e) {
            return back()->with('error', $e->getMessage());
        }

        return back()->with('error', __('Delete failed'));
    }
}